<!--基本散点图-->

<template>
  <div class="w-h-full">
    <!--组件遮罩层，用于在设计器中显示-->
    <div v-if="shade" class="shade"></div>

    <div v-resize="resizeHandle" style="width: 100%; height: 100%;">
      <div :id="'chart-'+component.id+'-'+chartDomNum" class="w-h-full"></div>
    </div>
  </div>
</template>

<script>

import FuncCompMixin from '@/mixin/FuncCompMixin'
import ChartCompMixin from '@/mixin/ChartCompMixin'
import PnDesigner from '@/utils/PnDesigner'
import BaseScatterChart from './BaseScatterChart'

const _this = {
  name: 'BaseScatterChart', // 此名称必须填写，且必须与组件文件名相同
  mixins: [FuncCompMixin, ChartCompMixin],
  attr: {
    version: '1.1',
    configDataTemp: Object.assign({

      // 组件相关配置属性，Echarts图表组件的相关配置属性，必须在chartOption对象下定义
      chartOption: {
        title: PnDesigner.buildTitleConfigData(),
        grid: PnDesigner.buildGridConfigData(),
        legend: PnDesigner.buildLegendConfigData(),
        tooltip: PnDesigner.buildTooltipConfigData(),
        xAxis: PnDesigner.buildXAxisConfigData({
          data: null
        }),
        yAxis: PnDesigner.buildYAxisConfigData(),
        series: [
          BaseScatterChart.buildSeriesObj('系列1')
        ]
      }

    }, PnDesigner.buildFuncCompDatasourceField({ // 如果组件需要配置数据源，则需要调用此函数构建数据源字段对象
      ds_resultObjTemplate: [
        {
          field: 'name',
          remark: '类目'
        },
        {
          field: 'value',
          remark: '值'
        },
        {
          field: 'series',
          remark: '系列'
        }
      ],
      ds_resultObj: [
        {name: '2021-01', value: 8, series: '系列1'},
        {name: '2021-02', value: 12, series: '系列1'},
        {name: '2021-03', value: 4, series: '系列1'},
        {name: '2021-04', value: 10, series: '系列1'},
        {name: '2021-05', value: 20, series: '系列1'}
      ],
      ds_useFilter: true, // 是否使用过滤器，默认不使用
      ds_useTimer: true, // 是否开启定时器配置，默认不开启
    }), PnDesigner.buildFuncCompGlobalField(), PnDesigner.buildEchartCompCustomOption(), PnDesigner.buildFuncCompInteractionConfiguration([
      PnDesigner.buildFuncCompInteractionStreamItem('itemClick', [
        PnDesigner.buildFuncCompInteractionFieldsMapItem('name', '', '类目'),
        PnDesigner.buildFuncCompInteractionFieldsMapItem('value', '', '值'),
        PnDesigner.buildFuncCompInteractionFieldsMapItem('series', '', '系列'),
      ])
    ]))
  },
  data() {
    return {}
  },
  mounted() {
    this.init();

    this.setTimer(() => { //定时器，如果当前组件不使用定时器，可以将此段代码删除
      this.init()
    })
  },
  methods: {
    init() { // 组价初始化函数，函数名必须使用init，不可更改
      this.initDatasource(() => {
        this.chart.setOption(this.chartOption);
        this.runCustomGlobalJsCode();
        this.runDataItemClickJsCode()
      }, (result) => {
        this.chart.setOption(this.chartOption);
        this.runCustomGlobalJsCode();
        this.runDataItemClickJsCode()
      });
    }
  },
  computed: {
    chartOption() {

      let option = {
        title: PnDesigner.buildTitleChartOption(this.component.compConfigData.chartOption.title),
        grid: PnDesigner.buildGridChartOption(this.component.compConfigData.chartOption.grid),
        legend: PnDesigner.buildLegendChartOption(this.component.compConfigData.chartOption.legend),
        tooltip: PnDesigner.buildTooltipChartOption(this.component.compConfigData.chartOption.tooltip),
        xAxis: PnDesigner.buildXAxisChartOption(this.component.compConfigData.chartOption.xAxis),
        yAxis: PnDesigner.buildYAxisChartOption(this.component.compConfigData.chartOption.yAxis),
        series: []
      };

      this.$PnUtil.groupArray(this.runCustomFilter(this.component.compConfigData.ds_resultObj), 'series').forEach((resultObjItem, index) => {
        if (resultObjItem) {
          let item = this.component.compConfigData.chartOption.series[index];
          if (item) {
            option.series.push({
              name: item.name ? item.name : resultObjItem.type,
              type: item.type,
              symbol: item.symbol,
              symbolSize: item.symbolSizeCallbackFunctionCode ? eval(item.symbolSizeCallbackFunctionCode) : item.symbolSize,
              symbolRotate: item.symbolRotate,
              label: {
                show: item.label.show,
                position: item.label.position,
                distance: item.label.distance,
                rotate: item.label.rotate,
                formatter: item.label.formatter ? eval(item.label.formatter) : (params)=>{return params.value},
                color: item.label.color,
                fontSize: item.label.fontSize
              },
              labelLine: {
                show: item.labelLine.show
              },
              itemStyle: {
                color: item.itemStyle.color,
                borderColor: item.itemStyle.borderColor,
                borderWidth: item.itemStyle.borderWidth,
                borderType: item.itemStyle.borderType,
                shadowColor: item.itemStyle.shadowColor,
                shadowBlur: item.itemStyle.shadowBlur,
                opacity: item.itemStyle.opacity
              },
              markLine: PnDesigner.buildMarkLineChartOption(item.markLine),
              markPoint: PnDesigner.buildMarkPointChartOption(item.markPoint),
              data: resultObjItem.list.map((item => {
                return [item.name, item.value]
              })),
            });
            option.legend.data.push(item.name ? item.name : resultObjItem.type)
          }else {
            option.series.push(BaseScatterChart.buildDefaultSeriesObj(resultObjItem.type, resultObjItem.list.map((item => {
              return [item.name, item.value]
            }))));
            option.legend.data.push(resultObjItem.type)
          }
        }
      })

      // 下面这一段代码用于合并组件的扩展配置，如果组件不需要扩展配置，可以删掉
      option = this.$PnDesigner.mergeChartCustomOption(option, this.component.compConfigData.customOption);

      return option;
    }
  }
};

export default _this
</script>

<style scoped>

</style>
